from tkinter import *
import os

class f1():
    def __init__(self):
        self.root = Tk()
        self.root.geometry('300x200+200+200')
        self.root.title('网络测试')
        self.var2 = StringVar()
        self.var1 = StringVar()
        self.var = StringVar()
    
    def com(self):
        ipaddr = self.var2.get()
        results = os.popen('ping %s'%ipaddr).read()
        print(results)
        if '丢失 = 0' and not '无法访问目标主机' in results:
            self.var.set('网络正常')
        elif 'TTL' in results:
            self.var.set('网络正常')
        elif '100% 丢失' in results:
            self.var.set('网络不通')
        elif '无法访问目标主机' in results:
            self.var.set('网络不通')
        elif '请求超时' in results:
            self.var.set('网络不通或有丢包')
        else:
            self.var.set('网络有丢包')

    def ip(self):
        ipchose = self.var1.get()
        if ipchose == 'ipv4':
            self.lable = Label(self.root,text='ipv4').pack()
            self.entry = Entry(self.root,textvariable=self.var2).pack()
            self.b1 = Button(self.root,text='确定',command=s1.com)
            self.b1.pack()
            self.con = Entry(self.root,textvariable=self.var)
            self.con.pack()
        else:
            self.lable1 = Label(self.root,text='ipv6').pack()
            self.entry1 = Entry(self.root,textvariable=self.var2).pack()
            self.b1 = Button(self.root,text='确定',command=s1.com)
            self.b1.pack()
            self.con = Entry(self.root,textvariable=self.var)
            self.con.pack()

    def win(self):
        self.r1 = Radiobutton(self.root,text='ipv4',font=12,height=2,value='ipv4',variable=self.var1,command=s1.ip)
        self.r1.pack()
        self.r2 = Radiobutton(self.root,text='ipv6',font=12,height=2,value='ipv6',variable=self.var1,command=s1.ip)
        self.r2.pack()

        self.root.mainloop()

s1 = f1()
s1.win()


